$(document).ready(function(){
    $('#requirement_options').change(function(){
        if ($("#requirement_options").val() != -1){
            addItemToListbox($("#requirement_options"), $("#requirement_list"));
            $("#requirement_options").val(-1);
        }
    });
    
    
    $('input').keyup(changeMade);
    $('select').change(changeMade);
    
    $('#cancel_button').click(function(){
        window.location = getBaseURL()+'settings/manage_users';
    });
    
    $('#submit_button').click(function(){
        if (validateData()){
            postUser();
        }
    });
    
    $("#input_username").keyup(function(){
        checkLogin($("#input_username").val(),$('#user_id').val());
    });
    
    $("#input_email").keyup(function(){
        checkEmail($("#input_email").val(),$('#user_id').val());
    });
    
    registerButtons();
});

function validateData(){
    var responses = new Object();
    var retval = true;
    if ($.trim($("#input_username").val()) == ""){
        responses["#username_section"] = "Username field is empty.";
        retval = false;
    }
    
    if ($.trim($("#input_email").val()) == ""){
        responses["#email_section"] = "Email field is empty.";
        retval = false;
    }
    
    if ($.trim($("#input_name").val()) == ""){
        responses["#name_section"] = "Name field is empty.";
        retval = false;
    }
    /*
    if ($("#input_password").val() == "" && $("#change_password").length == 0){
        responses["#password_section"] = "Password field is empty.";
        retval = false;
    }
    */
    highlightSections(responses);
    
    return retval;
}

function postUser(){
    var user_id = $("#user_id").val();
    var username = $("#input_username").val();
    var email = $("#input_email").val();
    var name = $("#input_name").val();
    var requirements = [];
    var password = $.trim($("#input_password").text());
    var change = $("#change_password").length;
    var edit = $('#no_requirements').val();
    if (edit == 1){
        requirements = [];
    }
    else{
        $("#requirement_list .listboxItem").each(function(){
            requirements.push($(this).attr('value')); // option id value
        });
    }
    
    
    
    $.post(getBaseURL()+"settings/create_user/postUser/",{
        user_id: user_id,
        username: username,
        email: email,
        name: name,
        requirements: requirements,
        edit: edit,
        password: password,
        change: change
    },
    function(data){
        if (data == "good"){
            saveMade();
            window.location = getBaseURL()+"settings/manage_users/";
        }
        else if (data == "update"){
            saveMade();
            window.location = getBaseURL()+"settings/manage_users/";
        }
        else if (data == "server"){
            saveMade();
            window.location = getBaseURL()+"settings/manage_users/";
            
        }else{
            var responses = new Object();
            
            if (data.login){
                responses['#username_section'] = data.login;
            }

            if (data.email){
                responses['#email_section'] = data.email;
            }
            
            highlightSections(responses);
        }
    },"json");
}

function checkLogin(user_login, user_id){
    $.post(getBaseURL()+"settings/create_user/checkLogin/",{
        user_login: user_login,
        user_id: user_id
    },
    function(data){
        var text = 'Not Unique';
        if (data == "good"){
            text = null;
        }
        toggleHighlight('#username_section', text);
    });
}

function checkEmail(user_email, user_id){
    $.post(getBaseURL()+"settings/create_user/checkEmail/",{
        user_email: user_email,
        user_id: user_id
    },
    function(data){
        var text = 'Not Unique';
        if (data == "good"){
            text = null;
        }
        toggleHighlight('#email_section', text);
    });
}

function registerButtons(){
    $("#cancel_password").click(function(){
        $("#password_location").html('<input type="button" class="buttonLight" id="change_password" value="Reset Password"/>');
        registerButtons();
    });
    
    $("#change_password").click(function(){
        $("#password_location").html('<div id="input_password">'+$("#hidden_pass").attr('value')+'</div>'+
        '<input type="button" id="cancel_password" class="buttonLight" value="Cancel"/>');
        
        registerButtons();
    });
}